<?
require_once $_SERVER['DOCUMENT_ROOT'] . "/kernel/iframe_header.php";
$MAIN->GetHeadStyles();
$MAIN->AddHeadStyle($MAIN->site['template_uri'] . "/less/template.less", "less");
Jscore::Init('less', 'jquery');

if (!$USER->isAdmin()) {
    Logger::error(t('Access denied'));
    die();
}
Module::Init('system.structure');
Module::Init('system.fields');
Logger::ShowAll();
$bid = $_REQUEST['bid'];
$entity = Fields::GetEntity($bid);
$fields = array();
$props = array();
$propObj = $MAIN->DB->native('show fields from bds__elements')->execute();

if (check_action('create_element', 'post')):
    $fieldsIn = isset($_POST['field']) ? $_POST['field'] : false;
    unset($_POST['field'], $_POST['action']);
    $props = array();
    foreach ($_POST as $f => $v) {
        $props[$f] = $v;
    }
    $props['date_create'] = date('Y-m-d H:i:s');
    foreach ($fieldsIn as $fid => $fval) {
        $in = array('field_id' => $fid, 'value' => $fval, 'parent_id' => $_POST['id'], 'relation' => 'E');
        FLDValues::Create($in);
    }
    $ok = BDSElement::Create($props);
    print t('Element created successful') . "<br />";
else:

    while ($prop = $propObj->Fetch()) {
        if ($prop['Field'] != 'id' && $prop['Field'] != 'date_update') {
            $props[] = $prop;
        }
    }

    foreach ($entity as $fld) {
        if ($fld['type'] == 'E') {
            $fields[] = $fld;
        }
    }

    $blockObj = BDSBlock::GetList(array(), array(), array('id', 'code'));
    $blocks = $blockObj->GetList();
    $sectObj = BDSSection::GetList(array(array('field' => 'block_id', 'value' => $bid)));
    $sections = $sectObj->GetList();
    $idObj = $MAIN->DB->native('SHOW TABLE STATUS LIKE "bds__elements"')->execute();
    $idD = $idObj->Fetch();
    $id = $idD['Auto_increment'];

    foreach ($sections as $i => $sec) {
        $sections[$i]['fields'] = Fields::GetFields($sec['id'], $bid, 'S');
    }
    $sections = BDSStructure::__tree($sections, 'section_id');
    ?>
    <form method="post" style="text-align: left">
        <div class="separator"><?=t('Properties')?></div>
        <? set_action('create_element') ?>
        <div class="formfield">
            <label for="block_id">Block code</label><select name="block_id">
                <?= GetOplionsList($blocks, 'id', 'code', array('id', $bid)); ?>
            </select></div>
        <div class="formfield">
            <label for="section_id">Section</label><select name="section_id">
                <?= GetOplionsList($sections, 'id', 'fields:name:value'); ?>
            </select>
        </div>
        <input type="hidden" name="id" value="<?= $id; ?>">
        <div class="formfield">
            <label for="code">Code</label><input type="text" name="code" value="">
        </div>
        <div class="formfield">
            <label for="sort">Sort</label><input type="text" name="sort" value="100">
        </div>
        <div class="separator"><?=t('Fields')?></div>
        <? foreach ($fields as $field): ?>
        <div class="formfield">
            <label for="<?= $field['code'] ?>"><?= $field['code'] ?></label><input type="text" name="field[<?= $field['id'] ?>]"><br>
            </div>
        <? endforeach; ?>
        <div class="formfield">
            <input type="submit">
        </div>
    </form>
<? endif; ?>